package com.eazykitchen.service;

import com.eazykitchen.dao.UserDao;
import com.eazykitchen.entity.Paginating;
import com.eazykitchen.entity.UserInformation;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Resource
    UserDao userDao;

    @Override
    public UserInformation getUserByName(String userName) throws Exception {
        try {
            return userDao.getUserByName(userName);
        }catch (Exception e){
            log.error("UserServiceImpl-->>getUserByName",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public UserInformation getUserById(int userId) throws Exception {
        try {
            UserInformation userInformation = userDao.getUserById(userId);
//            return userDao.getUserById(userId);
            return userInformation;
        }catch (Exception e){
            log.debug("UserServiceImpl-->>getUserById",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int addUser(UserInformation userInformation) throws Exception {
        try{
            return userDao.addUser(userInformation);
        }catch (Exception e){
            log.error("UserServiceImpl-->>addUser",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public boolean isUserName(String userName) throws Exception {
        try {
            UserInformation userInformation = userDao.getUserByName(userName);
            if (userInformation!=null) {
                return true;
            }
            else {
                return false;
            }
        } catch (Exception e) {
            log.debug("UserServiceImpl-->>isUserName",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int delUser(int userId) throws Exception {
        try {
            return userDao.delUser(userId);
        }catch (Exception e){
            log.debug("UserServiceImpl-->>delUser",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int updateUser(UserInformation userInformation) throws Exception {
        int flag = 0;
        try {
            userDao.updateUser(userInformation);
            flag = 1;
        } catch (Exception e) {
            log.debug("UserServiceImpl-->>updateUser", e);
            throw new RuntimeException(e.getMessage());
        }
        return flag;
    }

    @Override
    public Paginating<UserInformation> getUserList(UserInformation userInformation) throws Exception {
        try {
            PageHelper.startPage(userInformation.getPageNum(),userInformation.getPageSize());
            List<UserInformation> list = userDao.getUserList(userInformation);
            PageInfo<UserInformation> p = new PageInfo<>(list);
            return new Paginating<>(p);
        }catch (Exception e){
            log.debug("UserServiceImpl-->>getUserList",e);
            throw new RuntimeException(e.getMessage());
        }
    }


}
